package algorithm.graph;

import java.util.HashSet;
import java.util.Stack;

public class DFS {
    public static void dfs(Node node){
        if(node == null)
            return;
        Stack<Node> stack = new Stack<>();
        HashSet<Node> set = new HashSet<>();
        System.out.println(node.value);
        stack.push(node);
        set.add(node);
        while(!stack.isEmpty()){
            Node peek = stack.peek();
            for (Node next : peek.nexts) {
                if(!set.contains(next)){
                    stack.push(next);
                    set.add(next);
                    System.out.println(next.value);
                    break;
                }
            }
            if(stack.peek() == peek){
                stack.pop();
            }
        }

    }
}
